package cn.edu.henu.team.ui;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import androidx.annotation.NonNull;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.adapter.VoteAdapter;
import cn.edu.henu.team.base.BaseActivity;
import cn.edu.henu.team.bean.clinic;
import cn.edu.henu.team.utils.MyListview;

public class VoteActivity extends BaseActivity implements AdapterView.OnItemSelectedListener{
    private String zone = "#";
    private String city = "开封市";
    private ImageView back;
    private TextView vote;
    private MyListview lv;
    private EditText et_search;
    private SmartRefreshLayout refreshSrl;
    private ScrollView scrollView;
    private VoteAdapter mVoteAdapter;
    private List<clinic> mData = new ArrayList<clinic>();

    private Spinner mSpinner;
    private ArrayAdapter<String> zoneAdapter;
    private Set<String> mSetZone = new HashSet<>();
    private List<String> zoneData = new ArrayList<>();

    @Override
    protected int initLayout() {
        return R.layout.activity_vote;
    }

    @Override
    protected void initView() {
        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        lv = findViewById(R.id.lv);
        mVoteAdapter = new VoteAdapter(VoteActivity.this, mData);
        lv.setAdapter(mVoteAdapter);

        refreshSrl = findViewById(R.id.refresh_srl_book);
        refreshSrl.setRefreshHeader(new ClassicsHeader(getApplicationContext()));

        refreshSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshSrl.finishRefresh(1000/*,false*/);
                refreshData(zone);
            }
        });

        scrollView = findViewById(R.id.scroll);
        if (scrollView != null) {
            scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
                @Override
                public void onScrollChanged() {
                }
            });
        }

        et_search = findViewById(R.id.et_search);
        et_search.addTextChangedListener(new TextWatcher() {
            //每次EditText文本改变之前的时候，会回调这个方法
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //s     输入框中改变前的字符串信息
                //start 输入框中改变前的字符串的起始位置
                //count 输入框中改变前后的字符串改变数量一般为0
                //after 输入框中改变后的字符串与起始位置的偏移量
            }

            //每次EditText文本改变的时候，会回调这个方法
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //第一个参数s 的含义: 输入框中改变后的字符串信息
                //start 输入框中改变后的字符串的起始位置
                //before 输入框中改变前的字符串的位置 默认为0
                //count 输入框中改变后的一共输入字符串的数量
                zone = s.toString();
                refreshData(zone);
            }

            //每次EditText文本改变之后的时候，会回调这个方法
            @Override
            public void afterTextChanged(Editable s) {
                //edit  输入结束呈现在输入框中的信息
            }
        });

        mSpinner = findViewById(R.id.zone);
        // 加载数据
        zoneAdapter = new ArrayAdapter<String>(VoteActivity.this, android.R.layout.simple_spinner_item, zoneData);
        zoneAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 换风格
        mSpinner.setAdapter(zoneAdapter);   // 绑定数据
        mSpinner.setOnItemSelectedListener(this);
    }

    @Override
    protected void initData() {
        BmobQuery<clinic> query = new BmobQuery<clinic>();
        query.addWhereEqualTo("citys", city);
        query.findObjects(new FindListener<clinic>() {
            @Override
            public void done(List<clinic> lists, BmobException e) {
                if (e == null) {
                    if (lists.size() > 0) {
                        mData = lists;
                        for (clinic c : lists) {
                            mSetZone.add(c.getKinds());
                        }
                        for (String s : mSetZone) {
                            zoneData.add(s);
                        }

                        mVoteAdapter.addItem(mData);
                        mVoteAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    public void refreshData(String zone) {
        mData.clear();
        mSetZone.clear();
        zoneData.clear();

        if (zone.equals("#")) {
            initData();
        } else {
            BmobQuery<clinic> query = new BmobQuery<clinic>();
            query.addWhereEqualTo("kinds", zone);
            query.findObjects(new FindListener<clinic>() {
                @Override
                public void done(List<clinic> list, BmobException e) {
                    if (e == null) {
                        if (list.size() > 0) {
                            mData = list;
                            for (clinic c : list) {
                                mSetZone.add(c.getKinds());
                            }
                            for (String s : mSetZone) {
                                zoneData.add(s);
                            }

                            mVoteAdapter.addItem(mData);
                            mVoteAdapter.notifyDataSetChanged();
                        }
                    } else {
                        Toast.makeText(VoteActivity.this, "数据查询失败", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    /* 在子线程中更新UI ，实现自动填充验证码 */
    private void setTextInThread(EditText editText, String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                editText.setText(str);
            }
        });
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String str = parent.getItemAtPosition(position).toString();
        Log.d("++++++++++++++",str);
        Log.d("++++++++++++++",zoneData.get(0));
        setTextInThread(et_search,str);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}